package com.cwd.arm.application.service.write;

import com.cwd.arm.application.dto.project.ProjectAddDto;
import com.cwd.arm.application.dto.receiveNode.ReceiveNodeAddDto;
import com.cwd.arm.application.dto.receiveNode.ReceiveNodeUpdateDto;
import com.cwd.arm.application.service.write.facade.ReceiveNodeWriteService;
import com.cwd.arm.domain.entity.ProjectEntity;
import com.cwd.arm.domain.entity.ReceiveNodeEntity;
import com.cwd.arm.domain.repository.ProjectRepository;
import com.cwd.arm.domain.repository.ReceiveNodeRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Description:收款节点
* Created on 2021年11月16日
* @author  yan
*/
@Repository
@Primary
public class ReceiveNodeWriteServiceImpl implements ReceiveNodeWriteService{

    @Autowired
    private ReceiveNodeRepository receiveNodeRepository;

    @Override
    public Long saveReId(ReceiveNodeAddDto addDto) {
        return receiveNodeRepository.save(convertToEntity(addDto));
    }

    @Transactional
    @Override
    public void save(ReceiveNodeAddDto addDto) {
        List<ReceiveNodeAddDto> addDtoList = Arrays.asList(addDto);
        save(addDtoList);
    }

    @Override
    public void save(ProjectAddDto addDto) {
        receiveNodeRepository.save(convertToEntity(addDto),convertToEntityList(addDto.getReceiveNodeAddList()));
    }

    @Transactional
    @Override
    public void save(List<ReceiveNodeAddDto> addDtoList) {
        receiveNodeRepository.save(convertToEntityList(addDtoList));
    }

    @Transactional
    @Override
    public void update(ReceiveNodeUpdateDto updateDto) {
        ReceiveNodeEntity receiveNodeEntity = convertToEntity(updateDto);
        receiveNodeRepository.update(receiveNodeEntity);
    }

    @Override
    public void del(Long id) {
        receiveNodeRepository.del(id);
    }

    private List<ReceiveNodeEntity> convertToEntityList(List<ReceiveNodeAddDto> addDtoList){
        return addDtoList.stream().map(dto->(convertToEntity(dto))).collect(Collectors.toList());
    }

    private ReceiveNodeEntity convertToEntity(ReceiveNodeAddDto dto){
        ReceiveNodeEntity entity = new ReceiveNodeEntity();
        BeanUtils.copyProperties(dto,entity);
        entity.setCompletionRate(dto.getRate());
        return entity;
    }

    private ReceiveNodeEntity convertToEntity(ReceiveNodeUpdateDto dto){
        ReceiveNodeEntity entity = new ReceiveNodeEntity();
        BeanUtils.copyProperties(dto,entity);
        entity.setCompletionRate(dto.getRate());
        return entity;
    }

    private ProjectEntity convertToEntity(ProjectAddDto dto){
        ProjectEntity entity = new ProjectEntity();
        entity.setProjectTypeKey(dto.getProjectType());
        BeanUtils.copyProperties(dto,entity);
        return entity;
    }

}
